/**
 * 路由过滤掉 hide 为true的路由
 * @param {Array} routerList
 */
const routerFilter = (routerList = []) => {
    return routerList.filter(item => {
        if (item.hide) {
            return false;
        }
        if (item.children && item.children.length > 0) {
            item.children = routerFilter(item.children);
        }
        return true;
    });
};
/**
 *
 * @param {*} str
 */
const trim = str => {
    if (str == undefined) {
        return undefined;
    }
    str = str.replace(/^\s*|\s*$/g, '');
    return str;
};

/**
 *
 * @param {*} url
 * @param {*} saveName
 */
const openDownloadDialog = (url, saveName) => {
    if (typeof url == 'object' && url instanceof Blob) {
        url = URL.createObjectURL(url); // 创建blob地址
    }
    var aLink = document.createElement('a');
    aLink.href = url;
    aLink.download = saveName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
    var event;
    if (window.MouseEvent) event = new MouseEvent('click');
    else {
        event = document.createEvent('MouseEvents');
        event.initMouseEvent(
            'click',
            true,
            false,
            window,
            0,
            0,
            0,
            0,
            0,
            false,
            false,
            false,
            false,
            0,
            null
        );
    }
    aLink.dispatchEvent(event);
};
/**
 *
 * @param {*} base64Data
 * @param {*} type
 */
const dataURLtoBlob = (base64Data, type) => {
    let bstr = atob(base64Data),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: type });
};

/**
 * 路由加载方式修改
 */
const routerLoadModel = load => {
    return () => load;
    // const LOAD_MODE = process.env.VUE_APP_ROUTER_LOAD_MODE;
    // if (LOAD_MODE === 'LAZY') {
    //     return () => load;
    // } else {
    //     return  load;
    // }
};

export {
    routerFilter,
    trim,
    openDownloadDialog,
    dataURLtoBlob,
    routerLoadModel
};
